//
//  Utility.m
//  NewMonday
//
//  Created by  on 27/10/2011.
//  Copyright (c) 2011 广东薪传 . All rights reserved.
//

#import "Utility.h"
#import <QuartzCore/QuartzCore.h>
#import "Config.h"
#import "DefineAction.h"

@implementation Utility


+ (NSString*) getServerUrl{
    return  [NSString stringWithFormat:@"http://%@%@/api",SERVERURL,PORT];
}

+ (CGSize)getStringSize:(NSString *)string font:(UIFont *)font withMaxSize:(CGSize)maxSize {
	CGSize size = [string sizeWithFont:font constrainedToSize:maxSize lineBreakMode:UILineBreakModeWordWrap];
	return size;
}



+(int) CountUnicodeByteNumber:(NSString *) str{
	if(str==nil||[str isEqual:@""])return 0;
	int l = [str length];
	int c = 0;
	for (int i=0; i<l; i++) {
		if([str characterAtIndex:i]>0x80){//128
			c+=2;
		}else{
			c++;
		}
	}
	return c;
}

+(NSString *) trim:(NSString *) str{
   return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet ]];
}

+(BOOL) ValidateEmail: (NSString *) candidate 
{
	if(candidate==nil)return NO;
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}"; 
    NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
    return [emailTest evaluateWithObject:candidate]; 
}



+(void) customAlertWithMsg:(NSString*) msg andTitle:(NSString*)title andDelagate:(id)delegate{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                      message:msg
                                                     delegate:delegate
                                            cancelButtonTitle:@"关闭"
                                            otherButtonTitles:nil];
    [alertView show];

}
+(UIButton *)customButton:(id)target action:(SEL)action normalImage:(NSString*)normalImage lightedImage:(NSString*)lightedImage frame:(CGRect) frame title:(NSString *)title tag:(NSUInteger)tag{
    //UIImage *normal_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:normalImage ofType:nil]];
    UIImage *normal_Image=[UIImage imageNamed:normalImage];
    UIImage *lighted_Image=nil;
    if (lightedImage) {
        //lighted_Image=[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:lightedImage ofType:nil]];
        lighted_Image=[UIImage imageNamed:lightedImage];
    }else{
        lighted_Image=normal_Image;
    }
	UIButton *customButton = [UIButton buttonWithType:UIButtonTypeCustom];
    if (title!=nil) {
        customButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    }
    customButton.frame=frame;
    customButton.tag=tag;
    [customButton setTitle:title forState:UIControlStateNormal];
    customButton.titleLabel.font = [UIFont systemFontOfSize:18.0f];
	[customButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [customButton setImage:normal_Image forState:UIControlStateNormal];
    [customButton setImage:lighted_Image forState:UIControlStateHighlighted];
    [customButton setImage:lighted_Image forState:UIControlStateSelected];
    return customButton;
}
+(__autoreleasing NSString*) strFromDate:(NSString*) datestr{

   __autoreleasing NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *date=[dateFormatter dateFromString:[datestr stringByReplacingOccurrencesOfString:@"T" withString:@" "]];
    return [dateFormatter stringFromDate:date];
}

+(__autoreleasing NSString*) strFromSecond:(NSString*) second andFormat:(NSString*)format{
    
    __autoreleasing NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:format];
    NSDate *date=[NSDate dateWithTimeIntervalSince1970:[second longLongValue]/1000];
    return [dateFormatter stringFromDate:date];
}


/*图片显示通用方法*/
+(UIImage *) imageNamed:(NSString*) imageName{
    CGFloat scale = 1.0f;
    if([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
        scale = [[UIScreen mainScreen] scale];
    }
    if (UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPad) {
        return [UIImage imageNamed:imageName];
        /*if (scale>1) {
            imageName=[imageName stringByReplacingOccurrencesOfString:@"." withString:@"_ipad@2x."];
            return [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]].CGImage scale:scale orientation:UIImageOrientationUp];
        }else{
          imageName=[imageName stringByReplacingOccurrencesOfString:@"." withString:@"_ipad."];
            return [UIImage imageNamed:imageName];
        }*/
        
    }else{
        #if TARGET_IPHONE_SIMULATOR
            return [UIImage imageNamed:imageName];
        #elif TARGET_OS_IPHONE
        if (scale>1) {
            imageName=[imageName stringByReplacingOccurrencesOfString:@"." withString:@"@2x."];
        }else{
             imageName=[imageName stringByReplacingOccurrencesOfString:@"." withString:@"@2x."];
        }
            return [UIImage imageWithCGImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:imageName ofType:nil]].CGImage scale:scale orientation:UIImageOrientationUp];
        #endif
    }
    
}

/* 设置导航栏的标题 */
+ (UIView*)getTitleView:(NSString*)titleName {
    UIView *titleView=[[UIView alloc] initWithFrame:CGRectMake(0, 44, 200, 44)];
    titleView.backgroundColor=[UIColor clearColor];
    UILabel *titleLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 5, 180, 30)];
    titleLabel.textAlignment=NSTextAlignmentCenter;
    titleLabel.backgroundColor=[UIColor clearColor];
    titleLabel.textColor=[UIColor whiteColor];
    titleLabel.font=[UIFont fontWithName:FONT_FAMILY size:18.0];
    titleLabel.text = titleName;
    [titleView addSubview:titleLabel];
    return titleView;
}

/* 获取除了状态栏和导航栏外的内容显示的宽度 */
+ (float)getContentWidth {
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    return screenFrame.size.width;
}

/* 获取除了状态栏和导航栏外的内容显示的高度 */
+ (float)getContentHeight {    
    CGRect screenFrame = [UIScreen mainScreen].bounds;
    return screenFrame.size.height - 20 - 44;
}

//用户在圈子里的角色
+(NSString*) getUserRole:(int)type{
    switch (type) {
        case 1:{
            return @"创建者";
            break;
        }
        case 2:{
            return @"管理员";
            break;
        }
        case 3:{
            return @"成员";
            break;
        }
        case 4:{
            return @"关注者";
            break;
        }
        case 5:{
            return @"访客";
            break;
        }
    }
    return @"";
}

+(UILabel *) detailTextLabel:(CGRect) rect  andFontSize:(float) fontsize{
    UIFont *font=[UIFont fontWithName:FONT_FAMILY size:fontsize];
    UILabel *labeltemp=[[UILabel alloc] initWithFrame:rect];
    labeltemp.font=font;
    labeltemp.backgroundColor=[UIColor clearColor];
    return labeltemp;
}
+(NSString *) datefromSeconds:(NSString *) strvalue andDateFormat:(NSString *) dateFormat {
    NSDate *date=[NSDate  dateWithTimeIntervalSince1970:[strvalue longLongValue]/1000];
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init];//实例化一个NSDateFormatter对象
        [dateFormatter setDateFormat:dateFormat];//设定时间格式,这里可以设置成自己需要的格式
    NSString *result=[dateFormatter stringFromDate:date];
    return result;
}

@end
